﻿using Dgg.Anug.Cqrs.Core.Domain.Admin.Events;
using Dgg.Anug.Cqrs.Core.Infrastructure.Data;
using Dgg.Anug.Cqrs.Core.Infrastructure.Eventing;
using Raven.Client;

namespace Dgg.Anug.Cqrs.Core.Application.Admin.Services.Handlers
{
	public class SolutionCreatedPersister : IHandler<SolutionCreated>
	{
		private readonly IEventSession _eventSession;

		public SolutionCreatedPersister(IEventSession eventSession)
		{
			_eventSession = eventSession;
		}

		public bool Handle(SolutionCreated e)
		{
			_eventSession.Save(e);
			return true;
		}
	}

	public class SolutionCreatedQueryUpdater : IHandler<SolutionCreated>
	{
		private readonly IDocumentSession _modelSession;

		public SolutionCreatedQueryUpdater(IDocumentSession modelSession)
		{
			_modelSession = modelSession;
		}

		public bool Handle(SolutionCreated e)
		{
			_modelSession.Store(new Presentation.Models.Admin.Solution { Id = e.ReceiverId.ToString(), Name = e.Name });
			_modelSession.SaveChanges();
			return true;
		}
	}
}
